package DataStorage;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;

import HibernateSQLite.java.model.Critere;
import HibernateSQLite.java.model.CritereText;

public class CritereTextDAOHibernate extends
		GenericDAOHibernate<CritereText, Long, ICritereTextDAO> implements
		ICritereTextDAO {

	@SuppressWarnings("unchecked")
	public boolean isExist(String N_cordon, String critere_name) {
		try {
			List<CritereText> list = getSession()
					.createCriteria(CritereText.class)
					.add(Restrictions.eq("N_cordon", N_cordon))
					.createCriteria("critere")
					.add(Restrictions.eq("critere_name", critere_name)) // Foreign
					// key(clé
					// externe)
					.list();

			if (!list.isEmpty()) {
				return true;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}

		return false;
	}

	@SuppressWarnings("unchecked")
	public boolean isExist(long critereText_id) {
		try {
			List<CritereText> list = getSession()
					.createCriteria(CritereText.class)
					.add(Restrictions.eq("critereText_id", critereText_id))
					.list();
			if (!list.isEmpty()) {
				return true;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return false;
	}

	public boolean isExist_L1(CritereText critereText) {
		if (critereText.getL1().equals("")
				|| critereText.getL1().equals("0.00")) {
			return false;
		}
		return true;
	}

	public boolean isExist_L2(CritereText critereText) {
		if (critereText.getL2().equals("")
				|| critereText.getL2().equals("0.00")) {
			return false;
		}
		return true;
	}

	public boolean isExist_P1(CritereText critereText) {
		if (critereText.getP1().equals("")
				|| critereText.getP1().equals("0.00")) {
			return false;
		}
		return true;
	}

	public boolean isExist_P2(CritereText critereText) {
		if (critereText.getP1().equals("")
				|| critereText.getP1().equals("0.00")) {
			return false;
		}
		return true;
	}

	public boolean isExist_A(CritereText critereText) {
		if (critereText.getA().equals("") || critereText.getA().equals("0.00")) {
			return false;
		}
		return true;
	}

	public boolean isExist_B(CritereText critereText) {
		if (critereText.getB().equals("") || critereText.getB().equals("0.00")) {
			return false;
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	public CritereText getCritereText(String N_cordon, String critere_name) {
		try {
			List<CritereText> list = getSession()
					.createCriteria(CritereText.class)
					.add(Restrictions.eq("N_cordon", N_cordon))
					.createCriteria("critere")
					.add(Restrictions.eq("critere_name", critere_name)) // Foreign
					// key(clé
					// externe)
					.list();

			for (CritereText c : list) {
				return c;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<CritereText> getListCritereText(String critere_name) {
		try {
			List<CritereText> list = getSession()
					.createCriteria(CritereText.class)
					// .addOrder(Property.forName("N_cordon").asc() )
					.createCriteria("critere")
					.add(Restrictions.eq("critere_name", critere_name)).list();
			// Foreign key(clé externe)

			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;

	}

	@SuppressWarnings("unchecked")
	public List<CritereText> getListCritereText(long critere_id) {
		try {
			// Foreign key(clé externe)
			String hql = " from CritereText as ct where ct.critere.critere_id ="
					+ critere_id;
			// "select s.name from Student as s where s.tid ="+teacher.getId();
			Query q = getSession().createQuery(hql);
			List<CritereText> list = q.list();
			return list;

		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;

	}

	@SuppressWarnings("unchecked")
	public List<CritereText> getListCritereText(Critere critere) {
		try {
			String hql = "from CritereText as ct where ct.critere.critere_id ="
					+ critere.getCritere_id();
			// "select s.name from Student as s where s.tid ="+teacher.getId();
			Query q = getSession().createQuery(hql);
			List<CritereText> list = q.list();
			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	public List<String> getListCordon(String critere_name) {
		List<CritereText> listCritereText = this.getListCritereText(critere_name);
		List<String> list = new ArrayList<String>();
		for (CritereText c : listCritereText) {
			list.add(c.getN_cordon());
		}

		return list;
	}

	public int getNumberCordon(long critere_id) {
		List<CritereText> listCritereText = this.getListCritereText(critere_id);
		return listCritereText.size();
	}

	public List<String> getListDistinctOP(long critere_id) {
		List<CritereText> listCritereText = this.getListCritereText(critere_id);
		List<String> list = new ArrayList<String>();
		for (CritereText c : listCritereText) {
			if (!list.contains(c.getOP())) {
				list.add(c.getOP());
			}
		}

		return list;
	}

	public List<String> getDistinctCLASSE(long critere_id) {
		List<CritereText> listCritereText = this.getListCritereText(critere_id);
		List<String> list = new ArrayList<String>();
		for (CritereText c : listCritereText) {
			if (!list.contains(c.getCLASSE())) {
				list.add(c.getCLASSE());
			}
		}

		return list;
	}

	public List<String> getDistinctDESIGNATION(long critere_id) {
		List<CritereText> listCritereText = this.getListCritereText(critere_id);
		List<String> list = new ArrayList<String>();
		for (CritereText c : listCritereText) {
			if (!list.contains(c.getDESIGNATION())) {
				list.add(c.getDESIGNATION());
			}
		}

		return list;
	}

	public List<String> getDistinctMATIERES(long critere_id) {
		List<CritereText> listCritereText = this.getListCritereText(critere_id);
		List<String> list = new ArrayList<String>();
		for (CritereText c : listCritereText) {
			if (!list.contains(c.getMATIERES_1())) {
				list.add(c.getMATIERES_1());
			}
			if (!list.contains(c.getMATIERES_2())) {
				list.add(c.getMATIERES_2());
			}
		}

		return list;
	}

	public int getValidtyNumberByL1_L2_P1_P2_A_B(CritereText critereText) {
		int nb = 0;
		if (!critereText.getL1().equals("")
				&& !critereText.getL1().contentEquals("0.00")) {
			nb++;
		}
		if (!critereText.getL2().equals("")
				&& !critereText.getL2().contentEquals("0.00")) {
			nb++;
		}
		if (!critereText.getP1().equals("")
				&& !critereText.getP1().contentEquals("0.00")) {
			nb++;
		}
		if (!critereText.getP2().equals("")
				&& !critereText.getP2().contentEquals("0.00")) {
			nb++;
		}
		if (!critereText.getA().equals("")
				&& !critereText.getA().contentEquals("0.00")) {
			nb++;
		}
		if (!critereText.getB().equals("")
				&& !critereText.getB().contentEquals("0.00")) {
			nb++;
		}

		return nb;
	}

	public List<String> getListValidtyByL1_L2_P1_P2_A_B(CritereText critereText) {
		List<String> list = new ArrayList<String>();
		if (!critereText.getL1().equals("")
				&& !critereText.getL1().contentEquals("0.00")) {
			list.add("L1");
		}
		if (!critereText.getL2().equals("")
				&& !critereText.getL2().contentEquals("0.00")) {
			list.add("L2");
		}
		if (!critereText.getP1().equals("")
				&& !critereText.getP1().contentEquals("0.00")) {
			list.add("P1");
		}
		if (!critereText.getP2().equals("")
				&& !critereText.getP2().contentEquals("0.00")) {
			list.add("P2");
		}
		if (!critereText.getA().equals("")
				&& !critereText.getA().contentEquals("0.00")) {
			list.add("a");
		}
		if (!critereText.getB().equals("")
				&& !critereText.getB().contentEquals("0.00")) {
			list.add("b");
		}

		return list;
	}

	/*
	 * public void saveCritereText(String N_cordon, String OP, String classe,
	 * String config, String designation, String Matieres_1, String Matieres_2,
	 * String epaisseurs_1, String epaisseurs_2, String L1, String L2, String
	 * P1, String P2, String a, String b, String alpha, String beta, String u1,
	 * String u2, Critere critere) {
	 * 
	 * CritereText critereText = null; // long critere_id =
	 * critere.getCritere_id(); String critere_name = critere.getCritere_name();
	 * if (!this.isExist(N_cordon, critere_name)) { critereText = new
	 * CritereText();
	 * 
	 * critereText.setN_cordon(N_cordon); critereText.setOP(OP);
	 * critereText.setCLASSE(classe); critereText.setCONFIG(config);
	 * critereText.setDESIGNATION(designation);
	 * critereText.setMATIERES_1(Matieres_1);
	 * critereText.setMATIERES_2(Matieres_2);
	 * critereText.setEPAISSEURS_1(epaisseurs_1);
	 * critereText.setEPAISSEURS_2(epaisseurs_2); critereText.setL1(L1);
	 * critereText.setL2(L2); critereText.setP1(P1); critereText.setP2(P2);
	 * critereText.setA(a); critereText.setB(b); critereText.setALPHA(alpha);
	 * critereText.setBETA(beta); critereText.setU1(u1); critereText.setU2(u2);
	 * 
	 * // critere.getCritereText().add(critereText); //inserse = "false"
	 * critereText.setCritere(critere); // inverse = "true"
	 * getSession().save(critereText); } // this.commit();
	 * 
	 * // s'il y a sous-phase, programme peut marcher, mais il y a des //
	 * problemes // session.getTransaction().commit(); //
	 * HibernateUtil.closeSession(); }
	 */
	public void saveCritereText(String N_cordon, String OP, String classe,
			String config, String designation, String Matieres_1,
			String Matieres_2, String epaisseurs_1, String epaisseurs_2,
			String L1, String L2, String P1, String P2, String a, String b,
			String alpha, String beta, String u1, String u2, Critere critere) {

		CritereText critereText = null;
		// long critere_id = critere.getCritere_id();
		String critere_name = critere.getCritere_name();
		if (!this.isExist(N_cordon, critere_name)) {
			critereText = new CritereText();

			critereText.setN_cordon(N_cordon);
			critereText.setOP(OP);
			critereText.setCLASSE(classe);
			critereText.setCONFIG(config);
			critereText.setDESIGNATION(designation);
			critereText.setMATIERES_1(Matieres_1);
			critereText.setMATIERES_2(Matieres_2);
			critereText.setEPAISSEURS_1(epaisseurs_1);
			critereText.setEPAISSEURS_2(epaisseurs_2);
			critereText.setL1(L1);
			critereText.setL2(L2);
			critereText.setP1(P1);
			critereText.setP2(P2);
			critereText.setA(a);
			critereText.setB(b);
			critereText.setALPHA(alpha);
			critereText.setBETA(beta);
			critereText.setU1(u1);
			critereText.setU2(u2);

			// critere.getCritereText().add(critereText); //inserse = "false"
			critereText.setCritere(critere); // inverse = "true"
			getSession().save(critereText);
		}
		// this.commit();

		// s'il y a sous-phase, programme peut marcher, mais il y a des
		// problemes
		// session.getTransaction().commit();
		// HibernateUtil.closeSession();
	}

	public void deleteCritereText(CritereText critereText) {

		critereText.getCritere().getCritereText().remove(critereText);
		critereText.setCritere(null);

		getSession().delete(critereText);

		this.commit();
		this.closeSession();
	}

	@SuppressWarnings("unused")
	public static void main(String[] args) {
		CritereTextDAOHibernate critereTextDAO = new CritereTextDAOHibernate();
		CritereDAOHibernate critereDAO = new CritereDAOHibernate();
		Critere c1 = critereDAO.getCritere(1);
		Critere c2 = critereDAO.getCritere(2);

		CritereText ct1 = critereTextDAO.getCritereText("C10",
				"X95_Types 1,2,A,B,C");
		CritereText ct2 = critereTextDAO
				.getCritereText("C10", "X91_Essieu 2RD");

		System.out.println(ct1.getDESIGNATION());
		System.out.println(ct2.getDESIGNATION());

		System.out
				.println(critereTextDAO.isExist("C10", "X95_Types 1,2,A,B,C"));

	}

}
